import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/user',
    component: () => import('../views/Layout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: '/user/home'
      },
      {
        path: 'home',
        name: 'UserHome',
        component: () => import('../views/Home.vue')
      },
      {
        path: 'cart',
        name: 'UserCart',
        component: () => import('../views/Cart.vue')
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/Profile.vue')
      },
      {
        path: 'orders',
        name: 'UserOrders',
        component: () => import('../views/Orders.vue')
      },
      {
        path: 'announcements',
        name: 'UserAnnouncements',
        component: () => import('../views/Announcements.vue')
      }
    ]
  },
  {
    path: '/admin',
    component: () => import('../views/admin/Layout.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        redirect: '/admin/home'
      },
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('../views/admin/Home.vue')
      },
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('../views/admin/Profile.vue')
      },
      {
        path: 'categories',
        name: 'AdminCategories',
        component: () => import('../views/admin/Categories.vue')
      },
      {
        path: 'drugs',
        name: 'AdminDrugs',
        component: () => import('../views/admin/Drugs.vue')
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('../views/admin/Orders.vue')
      },
      {
        path: 'announcements',
        name: 'AdminAnnouncements',
        component: () => import('../views/admin/Announcements.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 检查并恢复登录状态
  if (!userStore.isAuthenticated) {
    userStore.checkAuth()
  }

  // 检查路由是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  const requiresAdmin = to.matched.some(record => record.meta.requiresAdmin)

  if (requiresAuth && !userStore.isAuthenticated) {
    // 需要登录但未登录，重定向到登录页
    next('/login')
    return
  }

  if (requiresAdmin && !userStore.isAdmin) {
    // 需要管理员权限但没有权限，重定向到用户首页
    next('/user/home')
    return
  }

  // 如果已登录用户访问登录页，重定向到对应的首页
  if (userStore.isAuthenticated && (to.path === '/login' || to.path === '/register')) {
    next(userStore.isAdmin ? '/admin/home' : '/user/home')
    return
  }

  next()
})

export default router 